package com.bolt.admin.module.log.service;

import com.bolt.admin.module.log.dto.LogDTO;
import com.bolt.admin.module.log.entity.LogEntity;
import com.bolt.admin.module.log.repository.LogRepository;
import com.bolt.admin.module.sys.entity.UserEntity;
import com.bolt.convention.data.PageInfo;
import com.bolt.support.base.service.BaseService;
import com.bolt.support.spring.jpa.jpql.Clauses;
import com.bolt.support.spring.jpa.repository.BasicJpaRepository;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2020-10-16 10:59:58
 *         系统日志
 **/
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class LogService extends BaseService <LogEntity, Long> {

    @Autowired
    private LogRepository logRepository;

    @Override
    protected BasicJpaRepository <LogEntity, Long> getRepository() {
        return logRepository;
    }

    public PageInfo <LogDTO> findPage(Pageable pageable, LogDTO logDTO) {
        return jpaQueryFactory.selectDistinct(LogDTO.class,
                Clauses.of(UserEntity::getRealName).as("userName"),
                Clauses.of(LogEntity::getUserId),
                Clauses.of(LogEntity::getId),
                Clauses.of(LogEntity::getBrowser),
                Clauses.of(LogEntity::getModule),
                Clauses.of(LogEntity::getMethod),
                Clauses.of(LogEntity::getRequestIp),
                Clauses.of(LogEntity::getDescription),
                Clauses.of(LogEntity::getCreateTime),
                Clauses.of(LogEntity::getSuccess),
                Clauses.of(LogEntity::getTime)
        )
                .from(Clauses.of(LogEntity.class))
                .innerJoin( Clauses.of(UserEntity.class))
                .on(Clauses.of(LogEntity::getUserId), Clauses.of(UserEntity::getId))
                .where(QueryHelp.getOperationClause(logDTO)).fetchPage(pageable);

    }


}
